package com.zhp.ggweather;

import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.zhp.ggweather.View.BladeView;
import com.zhp.ggweather.View.BladeView.OnItemClickListener;
import com.zhp.ggweather.View.PinnedHeaderListView;
import com.zhp.ggweather.adapter.CityAdapter;
import com.zhp.ggweather.adapter.SearchCityAdapter;
import com.zhp.ggweather.app.App;
import com.zhp.ggweather.model.City;

public class SearchFragment extends Fragment{

	private EditText ed_search_city;//搜索城市输入框
	private ImageButton ib_clear_search;//清空搜索框
	private View v_city_container;//城市列表视图
	private View v_search_container;//城市搜索结果视图
	private PinnedHeaderListView mCityListView;//顶部item悬浮视图
	private BladeView mLetter;//字母列表视图
	private ListView mSearchListView;//搜索结果列表视图
	private TextView tv_title_name;//标题名字
	private ImageView iv_back;//返回
	
	private SearchCityAdapter mSearchCityAdapter;
	private List<City> mCities;
	
	private CityAdapter mCityAdapter;
	// 首字母集
	private List<String> mSections;
	// 根据首字母存放数据
	private Map<String, List<City>> mMap;
	// 首字母位置集
	private List<Integer> mPositions;
	// 首字母对应的位置
	private Map<String, Integer> mIndexer;
	private App mApp;
	
	@SuppressLint("InflateParams")
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v=inflater.inflate(R.layout.layout_search, null);
		
		ed_search_city=(EditText) v.findViewById(R.id.search_et);
		ed_search_city.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				mSearchCityAdapter=new SearchCityAdapter(mCities, getActivity());
				mSearchListView.setAdapter(mSearchCityAdapter);
				mSearchListView.setTextFilterEnabled(true);
				if (mCities.size()<1||TextUtils.isEmpty(s)) {
					v_city_container.setVisibility(View.VISIBLE);
					v_search_container.setVisibility(View.INVISIBLE);
					ib_clear_search.setVisibility(View.GONE);
				}else {
					v_city_container.setVisibility(View.INVISIBLE);
					v_search_container.setVisibility(View.VISIBLE);
					ib_clear_search.setVisibility(View.VISIBLE);
					mSearchCityAdapter.getFilter().filter(s);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				
			}
		});
		ib_clear_search=(ImageButton) v.findViewById(R.id.search_btn_clear);
		ib_clear_search.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (!TextUtils.isEmpty(ed_search_city.getText().toString())) {
					ed_search_city.setText("");
				}
			}
		});
		v_city_container=v.findViewById(R.id.search_city_container);
		v_search_container=v.findViewById(R.id.search_content);
		mCityListView=(PinnedHeaderListView) v.findViewById(R.id.search_cities_list);
		mCityListView.setEmptyView(v.findViewById(R.id.search_cities_list_empty));
		mCityListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent=new Intent();
				City city=mCityAdapter.getItem(arg2);
				intent.putExtra("city", city);
				getActivity().setResult(getActivity().RESULT_OK, intent);
			}
		});
		mLetter=(BladeView) v.findViewById(R.id.search_cities_baldeview);
		mLetter.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(String s) {
				if (mIndexer.get(s)!=null) {
					mCityListView.setSelection(mIndexer.get(s));
				}
			}
		});
//		mLetter.setVisibility(View.GONE);
		mSearchListView=(ListView) v.findViewById(R.id.search_list);
		mSearchListView.setEmptyView(v.findViewById(R.id.search_empty));
		v_search_container.setVisibility(View.GONE);
		mSearchListView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return false;
			}
		});
		mSearchListView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent=new Intent();
				City city=mSearchCityAdapter.getItem(arg2);
				intent.putExtra("city", city);
				getActivity().setResult(getActivity().RESULT_OK, intent);
			}
		});
		tv_title_name=(TextView) v.findViewById(R.id.search_tv_name);
//		tv_title_name.setText(App.getInstance().getSharePreferenceUtil().getCity());
		iv_back=(ImageView) v.findViewById(R.id.search_iv_back);
		
//		initData();
		return v;
	}
	
	private void initData(){
		mApp=App.getInstance();
		mCities=mApp.getCityList();
		mSections=mApp.getSections();
		mMap=mApp.getMap();
		mPositions=mApp.getPositions();
		mIndexer=mApp.getIndexer();
		mCityAdapter=new CityAdapter(getActivity(), mCities, mMap, mSections, mPositions);
		mCityListView.setAdapter(mCityAdapter);
		mCityListView.setOnScrollListener(mCityAdapter);
		mCityListView.setPinnedHeaderView(LayoutInflater.from(getActivity()).inflate(R.layout.list_group_item, mCityListView,false));
		mLetter.setVisibility(View.VISIBLE);
	}
}